package org.jeecg.modules.lab.entrust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.entity.LabEntrustSample;
import org.jeecg.modules.lab.entrust.mapper.LabEntrustItemsMapper;
import org.jeecg.modules.lab.entrust.mapper.LabEntrustSampleMapper;
import org.jeecg.modules.lab.entrust.service.ILabEntrustSampleService;
import org.jeecg.modules.lab.entrust.vo.LabSamplePage;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 样品信息表
 * @Author: jeecg-boot
 * @Date: 2023-07-14
 * @Version: V1.0
 */
@Service
public class LabEntrustSampleServiceImpl extends ServiceImpl<LabEntrustSampleMapper, LabEntrustSample> implements ILabEntrustSampleService {

    @Autowired
    private LabEntrustSampleMapper labSampleMapper;

    @Autowired
    private LabEntrustItemsMapper labEntrustItemsMapper;

    @Autowired
    private LabEntrustSampleMapper labEntrustSampleMapper;

    @Override
    public List<LabEntrustSample> selectByMainId(String mainId) {
        return labSampleMapper.selectByMainId(mainId);
    }

    @Override
    public List<LabSamplePage> querySampleList(String samName, String samNum) {
        return labSampleMapper.querySampleList(samName, samNum);
    }

    @Override
    public void updateSample(String taskitemId) {
        LabEntrustItems labEntrustItems = labEntrustItemsMapper.selectById(taskitemId);
        if (labEntrustItems != null) {
            String samId = labEntrustItems.getSamId();
            LabEntrustSample labEntrustSample = labEntrustSampleMapper.selectById(samId);
            if (labEntrustSample != null && !"在检".equals(labEntrustSample.getSamStatus())) {
                labEntrustSample.setSamStatus("在检");
                labEntrustSample.setSynFlag("1");
                labEntrustSampleMapper.updateById(labEntrustSample);
            }
        }

    }

    @Override
    public void updateSamByTaskId(String taskId) {
        QueryWrapper<LabEntrustSample> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("entrust_id", taskId);
        queryWrapper.eq("sam_type", "主剂");
        List<LabEntrustSample> labEntrustSampleList = labEntrustSampleMapper.selectList(queryWrapper);
        if (labEntrustSampleList != null) {
            for (LabEntrustSample sample :labEntrustSampleList) {
                sample.setSamStatus("已检");
                labEntrustSampleMapper.updateById(sample);
            }
        }

    }

    @Override
    public List<LabSamplePage> getSampleList(String samNum, String samName) {
        return labEntrustSampleMapper.getSampleList(samNum,samName);
    }
}
